package com.cat.chapter051;

import com.sun.source.tree.BreakTree;

/**
 * @author cat
 * @description https://leetcode.cn/problems/koko-eating-bananas/
 * @create 2025/7/22 09:17
 * @since JDK17
 */

public class Solution01 {
    public boolean check(int[] p, int h, int k) {
        int ans = 0;
        for (int i = 0; i < p.length; i++) {
            ans += (p[i] + k - 1) / k;
        }
        return ans <= h;
    }

    public int minEatingSpeed(int[] piles, int h) {
        int l = -1, r = 0, n = piles.length, mid;
        for (int i = 0; i < n; i++) r = Math.max(r, piles[i]);
        r++;
        while (l + 1 < r) {
            mid = r - (r - l) / 2;
            if (check(piles, h, mid)) { // r
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
}